package Model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import Dao.DepartmentManageDao;
import Entity.Department;;

public class DepartmentManageModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private List<Department> departmentList = new Vector<Department>();
	DepartmentManageDao dao = new DepartmentManageDao();
	private String[] header = { "STT", "Department Name", "Department Allowance" };
	
//	public DepartmentManageModel() throws IOException, ClassNotFoundException,
//			SQLException {
//		departmentList = dao.getDepartmentList();
//	}
	public void loadDepartmentList() throws IOException, ClassNotFoundException, SQLException{
		departmentList = dao.getDepartmentList();
	}
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return header.length;
	}

	public int getRowCount() {
		// TODO Auto-generated method stub
		return departmentList.isEmpty() == true ? 0 : departmentList.size();
	}

//	public boolean isCellEditable(int rowIndex, int columnIndex) {
//		// TODO Auto-generated method stub
//		return super.isCellEditable(rowIndex, columnIndex);
//	}
//	
//	public Class<?> getColumnClass(int columnIndex) {
//		// TODO Auto-generated method stub
//		if (columnIndex == 0) {
//			return Integer.class;
//		}
//		return String.class;
//	}
	
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return header[column];
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		if (departmentList.isEmpty()) {
			return null;
		}
		Department department = departmentList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return rowIndex + 1;
		case 1:
			return department.getName();
		case 2:
			return department.getAllowance();
		}
		return null;
	}

	public void addDepartment(Department department) throws IOException,
			ClassNotFoundException, SQLException {
		dao.insert(department);
		departmentList.add(department);
		fireTableDataChanged();
	}

	public void editDepartment(int row, Department department) throws IOException, ClassNotFoundException, SQLException {
		departmentList.set(row, department);
		dao.update(department);
		fireTableDataChanged();
	}

	public void deleteDepartment(int row, Department department) throws IOException, ClassNotFoundException, SQLException {
		departmentList.remove(row);
		dao.Delete(department);
		fireTableDataChanged();
	}
}
